* "Ticking all the boxes"
* Eelco Harteveld
* Electoral Studies (2021)
* ========================

* Replication syntax for LISS

* Data: Politics & Values (w8-w19)
* Available from https://www.dataarchive.lissdata.nl/, under "8. Politics and Values"
* Background variables are available separately under "1. Background variables", these have been reshaped to yearly data in the file "Background by year.dta"

use "cv08a_1.1p_EN.dta", clear
merge 1:1 nomem_encr using "cv09b_2.1p_EN.dta", gen(m2)
merge 1:1 nomem_encr using "cv10c_EN_1.0p.dta", gen(m3)
merge 1:1 nomem_encr using "cv11d_EN_1.0p.dta", gen(m4)
merge 1:1 nomem_encr using "cv12e_EN_1.0p.dta", gen(m5)
merge 1:1 nomem_encr using "cv13f_EN_1.0p.dta", gen(m6)
merge 1:1 nomem_encr using "cv14g_EN_1.0p.dta", gen(m7)
merge 1:1 nomem_encr using "cv16h_EN_1.0p.dta", gen(m8)
merge 1:1 nomem_encr using "cv17i_EN_1.0p.dta", gen(m9)
merge 1:1 nomem_encr using "cv18j_EN_1.0p.dta", gen(m10)
merge 1:1 nomem_encr using "cv19k_EN_1.0p.dta", gen(m11)

* Rename sympathy variables
rename (cv08a076 cv09b076 cv10c076 cv11d176 cv12e176 cv13f215 cv14g215 cv16h215 cv17i215 cv18j215 cv19k215) (sy_CDA8 sy_CDA9 sy_CDA10 sy_CDA11 sy_CDA12 sy_CDA13 sy_CDA14 sy_CDA16 sy_CDA17 sy_CDA18 sy_CDA19)
rename (cv08a077 cv09b077 cv10c077 cv11d174 cv12e174 cv13f212 cv14g212 cv16h212 cv17i212 cv18j212 cv19k212) (sy_PvdA8 sy_PvdA9 sy_PvdA10 sy_PvdA11 sy_PvdA12 sy_PvdA13 sy_PvdA14 sy_PvdA16 sy_PvdA17 sy_PvdA18 sy_PvdA19)
rename (cv08a078 cv09b078 cv10c078 cv11d173 cv12e173 cv13f211 cv14g211 cv16h211 cv17i211 cv18j211 cv19k211) (sy_VVD8 sy_VVD9 sy_VVD10 sy_VVD11 sy_VVD12 sy_VVD13 sy_VVD14 sy_VVD16 sy_VVD17 sy_VVD18 sy_VVD19)
rename (cv08a085 cv09b085 cv10c085 cv11d175 cv12e175 cv13f213 cv14g213 cv16h213 cv17i213 cv18j213 cv19k213) (sy_PVV8 sy_PVV9 sy_PVV10 sy_PVV11 sy_PVV12 sy_PVV13 sy_PVV14 sy_PVV16 sy_PVV17 sy_PVV18 sy_PVV19)
rename (cv08a079 cv09b079 cv10c079 cv11d177 cv12e177 cv13f214 cv14g214 cv16h214 cv17i214 cv18j214 cv19k214) (sy_SP8 sy_SP9 sy_SP10 sy_SP11 sy_SP12 sy_SP13 sy_SP14 sy_SP16 sy_SP17 sy_SP18 sy_SP19)
rename (cv08a080 cv09b080 cv10c080 cv11d179 cv12e179 cv13f218 cv14g218 cv16h218 cv17i218 cv18j218 cv19k218) (sy_GL8 sy_GL9 sy_GL10 sy_GL11 sy_GL12 sy_GL13 sy_GL14 sy_GL16 sy_GL17 sy_GL18 sy_GL19)
rename (cv08a082 cv09b082 cv10c082 cv11d180 cv12e180 cv13f217 cv14g217 cv16h217 cv17i217 cv18j217 cv19k217) (sy_CU8 sy_CU9 sy_CU10 sy_CU11 sy_CU12 sy_CU13 sy_CU14 sy_CU16 sy_CU17 sy_CU18 sy_CU19)
rename (cv08a081 cv09b081 cv10c081 cv11d178 cv12e178 cv13f216 cv14g216 cv16h216 cv17i216 cv18j216 cv19k216) (sy_D668 sy_D669 sy_D6610 sy_D6611 sy_D6612 sy_D6613 sy_D6614 sy_D6616 sy_D6617 sy_D6618 sy_D6619)

* Rename ideological variables
rename (cv08a104 cv09b104 cv10c104 cv11d104 cv12e104 cv13f104 cv14g104 cv16h104 cv17i104 cv18j104 cv19k104) (immi8 immi9 immi10 immi11 immi12 immi13 immi14 immi16 immi17 immi18 immi19)
rename (cv08a101 cv09b101 cv10c101 cv11d101 cv12e101 cv13f101 cv14g101 cv16h101 cv17i101 cv18j101 cv19k101) (lr8 lr9 lr10 lr11 lr12 lr13 lr14 lr16 lr17 lr18 lr19)
rename (cv08a103 cv09b103 cv10c103 cv11d103 cv12e103 cv13f103 cv14g103 cv16h103 cv17i103 cv18j103 cv19k103) (redist8 redist9 redist10 redist11 redist12 redist13 redist14 redist16 redist17 redist18 redist19)

* Rename media
rename (cv08a002 cv09b002 cv10c002 cv11d002 cv12e002 cv13f002 cv14g002 cv16h002 cv17i002 cv18j002 cv19k002) (news_tv8 news_tv9 news_tv10 news_tv11 news_tv12 news_tv13 news_tv14 news_tv16 news_tv17 news_tv18 news_tv19)
rename (cv08a003 cv09b003 cv10c003 cv11d003 cv12e003 cv13f003 cv14g003 cv16h003 cv17i003 cv18j003 cv19k003) (news_online8 news_online9 news_online10 news_online11 news_online12 news_online13 news_online14 news_online16 news_online17 news_online18 news_online19)
rename (cv12e166 cv13f166 cv14g166 cv16h166 cv17i166 cv18j166 cv19k166) (news_paper12 news_paper13 news_paper14 news_paper16 news_paper17 news_paper18 news_paper19)

* Rename other variables
rename (cv12e199 cv13f199 cv14g199 cv16h199 cv17i199 cv18j199 cv19k199) (partisan12 partisan13 partisan14 partisan16 partisan17 partisan18 partisan19)
rename (cv12e200 cv13f200 cv14g200 cv16h200 cv17i200 cv18j200 cv19k200) (attrac12 attrac13 attrac14 attrac16 attrac17 attrac18 attrac19)
rename (cv08a012 cv09b012 cv10c012 cv11d012 cv12e012 cv13f012 cv14g012 cv16h012 cv17i012 cv18j012 cv19k012) (polint8 polint9 polint10 polint11 polint12 polint13 polint14 polint16 polint17 polint18 polint19)
rename (cv08a044 cv09b044 cv10c044 cv11d044 cv12e044 cv13f044 cv14g044 cv16h044 cv17i044 cv18j044 cv19k044) (swd8 swd9 swd10 swd11 swd12 swd13 swd14 swd16 swd17 swd18 swd19)

* Rename vote variables
{
recode cv08a058 cv09b058 cv10c058 cv11d171 cv12e171 cv13f209 cv14g209 cv16h209 cv17i244 cv18j308 cv19k308 (15/999=.)
recode cv08a058 (3=1) (nonmissing=0), gen(vt_CDA8)
recode cv09b058  (3=1) (nonmissing=0), gen(vt_CDA9)
recode cv10c058 (3=1) (nonmissing=0), gen(vt_CDA10)
recode cv11d171 (6=1) (nonmissing=0), gen(vt_CDA11)
recode cv12e171 (6=1) (nonmissing=0), gen(vt_CDA12)
recode cv13f209 (7=1) (nonmissing=0), gen(vt_CDA13)
recode cv14g209 (7=1) (nonmissing=0), gen(vt_CDA14)
recode cv16h209 (7=1) (nonmissing=0), gen(vt_CDA16)
recode cv17i244 (6=1) (nonmissing=0), gen(vt_CDA17)
recode cv18j308 (4=1) (nonmissing=0), gen(vt_CDA18)
recode cv19k308 (4=1) (nonmissing=0), gen(vt_CDA19)

recode cv08a058 (4=1) (nonmissing=0), gen(vt_PvdA8)
recode cv09b058  (4=1) (nonmissing=0), gen(vt_PvdA9)
recode cv10c058 (4=1) (nonmissing=0), gen(vt_PvdA10)
recode cv11d171 (4=1) (nonmissing=0), gen(vt_PvdA11)
recode cv12e171 (4=1) (nonmissing=0), gen(vt_PvdA12)
recode cv13f209 (4=1) (nonmissing=0), gen(vt_PvdA13)
recode cv14g209 (4=1) (nonmissing=0), gen(vt_PvdA14)
recode cv16h209 (4=1) (nonmissing=0), gen(vt_PvdA16)
recode cv17i244 (3=1) (nonmissing=0), gen(vt_PvdA17)
recode cv18j308 (8=1) (nonmissing=0), gen(vt_PvdA18)
recode cv19k308 (8=1) (nonmissing=0), gen(vt_PvdA19)

recode cv08a058 (5=1) (nonmissing=0), gen(vt_VVD8)
recode cv09b058 (5=1) (nonmissing=0), gen(vt_VVD9)
recode cv10c058 (5=1) (nonmissing=0), gen(vt_VVD10)
recode cv11d171 (3=1) (nonmissing=0), gen(vt_VVD11)
recode cv12e171 (3=1) (nonmissing=0), gen(vt_VVD12)
recode cv13f209 (3=1) (nonmissing=0), gen(vt_VVD13)
recode cv14g209 (3=1) (nonmissing=0), gen(vt_VVD14)
recode cv16h209 (3=1) (nonmissing=0), gen(vt_VVD16)
recode cv17i244 (2=1) (nonmissing=0), gen(vt_VVD17)
recode cv18j308 (2=1) (nonmissing=0), gen(vt_VVD18)
recode cv19k308 (2=1) (nonmissing=0), gen(vt_VVD19)

recode cv08a058 (12=1) (nonmissing=0), gen(vt_PVV8)
recode cv09b058 (12=1) (nonmissing=0), gen(vt_PVV9)
recode cv10c058 (12=1) (nonmissing=0), gen(vt_PVV10)
recode cv11d171 (5=1) (nonmissing=0), gen(vt_PVV11)
recode cv12e171 (5=1) (nonmissing=0), gen(vt_PVV12)
recode cv13f209 (5=1) (nonmissing=0), gen(vt_PVV13)
recode cv14g209 (5=1) (nonmissing=0), gen(vt_PVV14)
recode cv16h209 (5=1) (nonmissing=0), gen(vt_PVV16)
recode cv17i244 (4=1) (nonmissing=0), gen(vt_PVV17)
recode cv18j308 (3=1) (nonmissing=0), gen(vt_PVV18)
recode cv19k308 (3=1) (nonmissing=0), gen(vt_PVV19)

recode cv08a058 (6=1) (nonmissing=0), gen(vt_SP8)
recode cv09b058 (6=1) (nonmissing=0), gen(vt_SP9)
recode cv10c058 (6=1) (nonmissing=0), gen(vt_SP10)
recode cv11d171 (7=1) (nonmissing=0), gen(vt_SP11)
recode cv12e171 (7=1) (nonmissing=0), gen(vt_SP12)
recode cv13f209 (6=1) (nonmissing=0), gen(vt_SP13)
recode cv14g209 (6=1) (nonmissing=0), gen(vt_SP14)
recode cv16h209 (6=1) (nonmissing=0), gen(vt_SP16)
recode cv17i244 (5=1) (nonmissing=0), gen(vt_SP17)
recode cv18j308 (7=1) (nonmissing=0), gen(vt_SP18)
recode cv19k308 (7=1) (nonmissing=0), gen(vt_SP19)

recode cv08a058 (7=1) (nonmissing=0), gen(vt_GL8)
recode cv09b058 (7=1) (nonmissing=0), gen(vt_GL9)
recode cv10c058 (7=1) (nonmissing=0), gen(vt_GL10)
recode cv11d171 (9=1) (nonmissing=0), gen(vt_GL11)
recode cv12e171 (9=1) (nonmissing=0), gen(vt_GL12)
recode cv13f209 (10=1) (nonmissing=0), gen(vt_GL13)
recode cv14g209 (10=1) (nonmissing=0), gen(vt_GL14)
recode cv16h209 (10=1) (nonmissing=0), gen(vt_GL16)
recode cv17i244 (9=1) (nonmissing=0), gen(vt_GL17)
recode cv18j308 (6=1) (nonmissing=0), gen(vt_GL18)
recode cv19k308 (6=1) (nonmissing=0), gen(vt_GL19)

recode cv08a058 (9=1) (nonmissing=0), gen(vt_CU8)
recode cv09b058 (9=1) (nonmissing=0), gen(vt_CU9)
recode cv10c058 (9=1) (nonmissing=0), gen(vt_CU10)
recode cv11d171 (10=1) (nonmissing=0), gen(vt_CU11)
recode cv12e171 (10=1) (nonmissing=0), gen(vt_CU12)
recode cv13f209 (9=1) (nonmissing=0), gen(vt_CU13)
recode cv14g209 (9=1) (nonmissing=0), gen(vt_CU14)
recode cv16h209 (9=1) (nonmissing=0), gen(vt_CU16)
recode cv17i244 (8=1) (nonmissing=0), gen(vt_CU17)
recode cv18j308 (9=1) (nonmissing=0), gen(vt_CU18)
recode cv19k308 (9=1) (nonmissing=0), gen(vt_CU19)

recode cv08a058 (8=1) (nonmissing=0), gen(vt_D668)
recode cv09b058 (8=1) (nonmissing=0), gen(vt_D669)
recode cv10c058 (8=1) (nonmissing=0), gen(vt_D6610)
recode cv11d171 (8=1) (nonmissing=0), gen(vt_D6611)
recode cv12e171 (8=1) (nonmissing=0), gen(vt_D6612)
recode cv13f209 (8=1) (nonmissing=0), gen(vt_D6613)
recode cv14g209 (8=1) (nonmissing=0), gen(vt_D6614)
recode cv16h209 (8=1) (nonmissing=0), gen(vt_D6616)
recode cv17i244 (7=1) (nonmissing=0), gen(vt_D6617)
recode cv18j308 (5=1) (nonmissing=0), gen(vt_D6618)
recode cv19k308 (5=1) (nonmissing=0), gen(vt_D6619)
}

* Empty up
keep sy_* vt_* immi* lr* redist* partisan* attrac* polint* swd* news* nomem_encr 

* Reshape
gen id = _n
reshape long sy_CDA sy_PvdA sy_VVD sy_PVV sy_SP sy_GL sy_CU sy_D66 vt_CDA vt_PvdA vt_VVD vt_PVV vt_SP vt_GL vt_CU vt_D66 immi lr redist partisan attrac polint swd ///
		news_online news_tv news_paper ///
		, i(id) j(wave)
recode wave (8 = 2007) (9=2008) (10=2009) (11=2010) (12=2011) (13=2012) (14=2013) (15=2014) (16=2015) (17=2016) (18=2017) (19=2018), gen(year)
sort id wave 

*************
* RECODINGS *
*************

merge m:m nomem_encr year using "Background by year.dta", gen(mback) update replace
xtset id wave

* Missing values
recode sy_* (999=.)
recode immi lr redist (99 999=.)

* Vote choice
gen vote = .
replace vote=1 if vt_CDA==1
replace vote=2 if vt_PvdA==1
replace vote=3 if vt_VVD==1
replace vote=4 if vt_PVV==1
replace vote=5 if vt_SP==1
replace vote=6 if vt_GL==1
replace vote=7 if vt_CU==1
replace vote=8 if vt_D66==1
lab define vote 1 "CDA" 2 "PvdA" 3 "VVD" 4 "PVV" 5 "SP" 6 "GL" 7 "CU" 8 "D66"
lab values vote vote
rename (vt_CDA-vt_D66) (vt_1 vt_2 vt_3 vt_4 vt_5 vt_6 vt_7 vt_8)

* Ideology
recode immi (1/2=1 "Pro-immigrant") (3=2) (4=3) (5=4 "Anti-immigrant"), gen(immi4)
recode redist (1/2=1 "Against redistribution") (3=2) (4=3) (5=4 "In favor of redistribution"), gen(redist4)
recode lr (0/2=1 "Far left") (3/4=2 "Moderate left") (5=3 "Center") (6/7=4 "Moderate right") (8/10=5 "Far right"), gen(lr5)
egen std_immi = std(immi)
egen std_lr = std(lr)

* SWD
recode swd (11/99=.)

* Background
rename lftdcat age_full
rename geslacht gender
rename sted urb
rename oplcat edu
recode herkomstgroep (0=0) (nonmissing=1), gen(immigr)
recode nettocat (6/8=6) (9/12=7) (13/14=8 "No data"), gen(inc7)
recode leeftijd (0/15=1) (16/20=2) (21/25=3) (26/30=4) (31/35=5) (36/40=6) (41/45=7) (46/50=8) (51/55=9) (56/60=10) (11/65=11) (66/70=12) (71/999=13), gen(age)

foreach var of varlist age_full gender urb edu immigr inc7 age {
	replace `var' = l.`var' if `var'==.
	}

* Sortedness
*tab vote, gen(vt_)

* - individual - 1
egen edu3 = cut(edu), group(3)
egen urb3 = cut(urb), group(3)
egen age3 = cut(age), group(3)
egen inc3 = cut(inc7), group(3)

set matsize 11000
local vars = "i.age3 i.urb3 i.edu3 i.inc3"
local vars2 = "age urb edu inc7"
capture drop pred
capture drop sorting
gen pred = .
foreach w of numlist 8 9 10 11 12 13 14 16 17 18 19 {
	forvalues p = 1/8 {
		display `w' ", " `p'
        regress vt_`p' `vars' if wave == `w'
		predict pred_tmp, resid
		replace pred = abs(pred_tmp) if vote==`p' & wave==`w'
		drop pred_tmp
		}
	}
gen sorting = 1 - pred

* Size of parties 
rename  (vt_1 vt_2 vt_3 vt_4 vt_5 vt_6 vt_7 vt_8) (vt_CDA vt_PvdA vt_VVD vt_PVV vt_SP vt_GL vt_CU vt_D66)
bys wave: egen si_CDA = mean(vt_CDA)
bys wave: egen si_PvdA = mean(vt_PvdA)
bys wave: egen si_VVD = mean(vt_VVD)
bys wave: egen si_PVV = mean(vt_PVV)
bys wave: egen si_SP = mean(vt_SP)
bys wave: egen si_GL = mean(vt_GL)
bys wave: egen si_CU = mean(vt_CU)
bys wave: egen si_D66 = mean(vt_D66)

* Affective polarization à la Wagner
* - unweighted
egen nm_symp = rownonmiss(sy_*)
egen mn_symp = rowmean(sy_*)
local vars = "CDA PvdA VVD PVV SP GL CU D66"
foreach var of newlist `vars' {
	gen comp_`var' = (sy_`var' - mn_symp)^2/nm_symp
	}
egen comp = rowtotal(comp_*), missing
gen UAP = sqrt(comp)
drop comp*

* - weighted
local vars = "CDA PvdA VVD PVV SP GL CU D66"
foreach var of newlist `vars' {
	gen wgt_sy_`var' = sy_`var'*si_`var'
	}
egen wgt_sy = rowtotal(wgt_sy_*), missing
foreach var of newlist `vars' {
	gen comp_`var' = si_`var'*(sy_`var'-wgt_sy)^2
	}
egen comp = rowtotal(comp_*), missing
gen WAP = sqrt(comp)
drop comp*

* Ideological polarization
* - SD
sum lr if wave==8
local mn_lr = r(mean)
local sd_lr = r(sd)
sum immi if wave==8
local mn_immi = r(mean)
local sd_immi = r(sd)
sum redist if wave==8
local mn_redist = r(mean)
local sd_redist = r(sd)
gen extr_lr = abs(lr-`mn_lr')
gen extr_immi = abs(lr-`mn_immi')
gen extr_redist = abs(lr-`mn_redist')
capture drop std_lr 
capture drop std_immi 
capture drop std_redist
gen std_immi = (immi-`mn_immi')/`sd_immi'
gen std_lr = (lr-`mn_lr')/`sd_lr'
gen std_redist = (redist-`mn_redist')/`sd_redist'

* Robustness measuress
gen WAP_nonmiss = .
replace WAP_nonmiss = sqrt( ///
			si_CDA*(sy_CDA-wgt_sy)^2 + si_PvdA*(sy_PvdA-wgt_sy)^2 + si_VVD*(sy_VVD-wgt_sy)^2 ///
			+ si_PVV*(sy_PVV-wgt_sy)^2 + si_SP*(sy_SP-wgt_sy)^2 + si_GL*(sy_GL-wgt_sy)^2 ///
			+ si_CU*(sy_CU-wgt_sy)^2 + si_D66*(sy_D66-wgt_sy)^2 ///
			)

* Mean scores for trend lines
bys wave: egen sd_immi=sd(std_immi)
bys wave: egen sd_lr=sd(std_lr)
bys wave: egen sd_redist=sd(std_redist)
bys wave: egen mn_WAP = mean(WAP)
bys wave: egen mn_mn_symp = mean(mn_symp)
bys wave: egen mn_sorting = mean(sorting)

* Standardize
egen std_WAP = std(WAP)
egen std_sorting = std(sorting)

* Lag
xtset id wave
gen lstd_WAP = l.std_WAP 
gen llr5 = l.lr5

* Labels
lab values vote vote
lab values lr5 lr5
lab values llr5 lr5
lab define edu 1 "Primary" 2 "L-Secondary" 3 "H-Secondary" 4 "L-Vocational" 5 "H-Vocational" 6 "University"
lab values edu edu
lab define age_full 1 "<14" 2 "15-24" 3 "25-34" 4 "34-44" 5 "45-54" 6 "55-64" 7 "65+"
lab values age_full age_full
lab var lr5 "Ideology"
lab var llr5 "Ideology"
lab var age_full "Age"
lab define gender 1 "Male" 2 "Female"
lab values gender gender
lab var gender "Gender"
lab var edu "Education"
lab var sorting "Social sorting"
lab var age "Age"
lab var gender "Female"

************
* ANALYSIS *
************

xtset id wave
tab lr5, gen(lr5_) 
tab polint, gen(polint_)
tab edu, gen(edu_r_)

* Descriptives
* ------------

xtsum WAP sorting age lr5_* polint_* edu_r_* if e(sample)

* Regressions
* -----------

* Main: hybrid
recode edu (5 6=1 "High educated") (nonmissing=0 "Low/middle educated"), gen(highedu)
local IVs "sorting lr5_1 lr5_2 lr5_4 lr5_5 polint_1 polint_3 highedu gender age"
xthybrid std_WAP `IVs', clusterid(id) se star
estout using "Main model hybrid.xls", stats(N) cells(b(star fmt(3)) se(par(`"="("' `")""') fmt(3))) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)  varlabels(_cons Intercept) noomit nobase label varwidth(40) replace

coefplot, ///
	order(W_* B_* R_*) ///
	heading( ///
		   W__sorting="{bf:Within-respondents}" B__sorting="{bf:Between-respondents}" ///
		   W__lr5_1=" " W__lr5_4="{it:Centrist (ref)}" ///
		   B__lr5_1=" " B__lr5_4="{it:Centrist (ref)}" ///
		   W__polint_1=" "  W__polint_3="{it:Medium political interest (ref)}" ///
		   B__polint_1=" "  B__polint_3="{it:Medium political interest (ref)}" ///
		   W__highedu=" " B__highedu=" " W__age=" " B__age=" " R__gender=" " ///
		   , gap(0)) ///
	coeflabels( ///
		   W__lr5_1="Far left (W)" W__lr5_2="Center left (W)" W__lr5_4="Center right (W)" W__lr5_5="Far right (W)" ///
	       W__polint_1="Low political interest (W)" W__polint_3="High political interest(W)" W__highedu="High educated (W)" W__age="Age (W)" W__sorting="Social sorting (W)" ///
		   B__lr5_1="Far left (B)" B__lr5_2="Center left (B)" B__lr5_4="Center right (B)" B__lr5_5="Far right (B)" ///
	       B__polint_1="Low political interest(B)" B__polint_3="High political interest (B)" B__highedu="High educated (B)" B__age="Age (B)" B__sorting="Social sorting (B)" R__gender="Female (B)") ///
		   drop(_cons) ///
		   scheme(cleanplots) ///
		   ysize(8) xline(0) xlabel(-0.2(0.2)1)
cd "c:\users\ehartev1\Dropbox\From disagree to disapprove\Text\Social sorting and AP in Europe\Submission 2\Proof"
graph export "Figure 3.tif", replace width(3150)

* Alternative: extremity measure
gen extremity = abs(5-lr)
egen std_extremity = std(extremity) 
local IVs "std_sorting std_extremity ib2.polint"
xtreg std_WAP `IVs', fe
estimates store FE
xtreg std_WAP `IVs' i.edu gender age, be
estimates store BE
coefplot FE BE, drop(_cons) scheme(plotplainblind) xline(0) ///
	legend(order(2 "Within individuals (FE)" 4 "Between individuals (BE)")) name(gr1, replace) title("") ///
	headings(1.lr5 = "{bf:Ideology} (ref: center)" 1.polint = "{bf:Political interest} (ref: fairly)" 2.edu = "{bf: Education} (ref: primary)" gender = "{bf: Other}") 
graph export "REG std_API coefplot fe std_extremity.png", replace
estout FE BE using "Main models CSES std_extremity.xls", stats(N N_g) cells(b(star fmt(3)) se(par(`"="("' `")""') fmt(3))) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)  varlabels(_cons Intercept) noomit nobase label varwidth(40) replace

* Robustness: FD
xtset id wave
local IVs "d.sorting ib3.lr5 ib2.polint"
xtreg d.WAP `IVs' i.edu gender age, fe
estout using "Main models LISS - FD.xls", stats(N N_g) cells(b(star fmt(3)) se(par(`"="("' `")""') fmt(3))) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001)  varlabels(_cons Intercept) noomit nobase label varwidth(40) replace